package cn.iocoder.yudao.module.erp.dal.mysql.deppurchaseitem;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.erp.controller.admin.deppurchaseitem.vo.DepPurchaseItemPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.deppurchaseitem.vo.DepPurchaseItemSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.deppurchaseitem.DepPurchaseItemDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * ERP 科室采购申请项 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface DepPurchaseItemMapper extends BaseMapperX<DepPurchaseItemDO> {

    default PageResult<DepPurchaseItemDO> selectPage(DepPurchaseItemPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DepPurchaseItemDO>()
                .eqIfPresent(DepPurchaseItemDO::getOrderId, reqVO.getOrderId())
                .eqIfPresent(DepPurchaseItemDO::getSupplierId, reqVO.getSupplierId())
                .eqIfPresent(DepPurchaseItemDO::getProductId, reqVO.getProductId())
                .eqIfPresent(DepPurchaseItemDO::getProductUnit, reqVO.getProductUnitId())
                .eqIfPresent(DepPurchaseItemDO::getProductStandards, reqVO.getProductStandards())
                .eqIfPresent(DepPurchaseItemDO::getProductPrice, reqVO.getProductPrice())
                .eqIfPresent(DepPurchaseItemDO::getCount, reqVO.getCount())
                .eqIfPresent(DepPurchaseItemDO::getCountActual, reqVO.getCountActual())
                .eqIfPresent(DepPurchaseItemDO::getNum, reqVO.getNum())
                .eqIfPresent(DepPurchaseItemDO::getTotalPrice, reqVO.getTotalPrice())
                .eqIfPresent(DepPurchaseItemDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(DepPurchaseItemDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(DepPurchaseItemDO::getId));
    }
    default PageResult<DepPurchaseItemDO> selectPageNew(DepPurchaseItemPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DepPurchaseItemDO>()
                .inIfPresent(DepPurchaseItemDO::getOrderId, reqVO.getIds())
                .eqIfPresent(DepPurchaseItemDO::getSupplierId, reqVO.getSupplierId())
                .eqIfPresent(DepPurchaseItemDO::getProductId, reqVO.getProductId())
                .eqIfPresent(DepPurchaseItemDO::getProductUnit, reqVO.getProductUnitId())
                .eqIfPresent(DepPurchaseItemDO::getProductStandards, reqVO.getProductStandards())
                .eqIfPresent(DepPurchaseItemDO::getProductPrice, reqVO.getProductPrice())
                .eqIfPresent(DepPurchaseItemDO::getCount, reqVO.getCount())
                .eqIfPresent(DepPurchaseItemDO::getCountActual, reqVO.getCountActual())
                .eqIfPresent(DepPurchaseItemDO::getNum, reqVO.getNum())
                .eqIfPresent(DepPurchaseItemDO::getTotalPrice, reqVO.getTotalPrice())
                .eqIfPresent(DepPurchaseItemDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(DepPurchaseItemDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(DepPurchaseItemDO::getId));
    }

    default List<DepPurchaseItemDO> selectListByOrderId(Long orderId) {
        return selectList( new LambdaQueryWrapperX<DepPurchaseItemDO>().eq(DepPurchaseItemDO::getOrderId, orderId).eq(DepPurchaseItemDO::getDeleted, 0));
    }

    default int deleteByOrderId(Long orderId) {
        return delete(DepPurchaseItemDO::getOrderId, orderId);
    }

    @Update(" update erp_dep_purchase_item set count_actual =  #{countActual} where id = #{id}")
    void updatecount(DepPurchaseItemSaveReqVO updateReqVO);

    @Update(" update erp_dep_purchase_item set nums =  #{nums} where id = #{id}")
    void updatecounts(DepPurchaseItemSaveReqVO updateReqVO);



    @Select("SELECT  yiji.count as num,z.id, z.order_id,z.nums,z.supplier_id,z.product_id,z.product_unit_id,z.product_unit_name,z.product_standards,z.product_price,z.count,z.count_actual,z.total_price,z.remark,z.tenant_id,z.product_name,z.creator,z.create_time,z.updater,z.update_time,z.deleted,z.tenant_id\n" +
            "FROM erp_dep_purchase sq\n" +
            "LEFT JOIN erp_dep_purchase_item z ON sq.id = z.order_id\n" +
            "LEFT JOIN (select product_id,sum(count) as count  from erp_pri_stock GROUP BY product_id) yiji ON z.product_id = yiji.product_id " +
            "WHERE z.order_id  =  #{orderId} and z.deleted = 0")
    List<DepPurchaseItemDO> selectByOrderId(Long orderId);


    @Select("select   edpi.id id ,edpi.order_id order_id, edpi.product_category_id product_category_id, edpi.product_category_name product_category_name, edpi.supplier_id supplier_id,edpi.product_id product_id,\n" +
            "edpi.product_unit_id product_unit_id,edpi.product_unit_name product_unit_name,edpi.product_standards product_standards,edpi.product_price product_price,\n" +
            "edpi.count_actual count_actual,edpi.total_price total_price,edpi.remark remark,edpi.product_name product_name,edpi.supplier_dept_id supplier_dept_id\n" +
            "from erp_dep_purchase edp\n" +
            "left join erp_dep_purchase_item edpi on edp.id = edpi.order_id\n" +
            "where  edp.integrate_id = #{integraId} and edpi.deleted = 0 and edpi.status = 0")
    List<DepPurchaseItemDO> getDepPurchaseItemByPriWarehouseIntegraId(String integraId);

    @Select("select  edpi.product_category_id product_category_id,edpi.product_category_name product_category_name ,edpi.supplier_id supplier_id,  edpi.product_unit_id product_unit_id, edpi.product_unit_name product_unit_name, edpi.product_name product_name," +
            "edpi.product_standards product_standards, edpi.product_id product_id, sum(edpi.count_actual) count_actual\n" +
            "from erp_dep_purchase edp\n" +
            "left join erp_dep_purchase_item edpi on edp.id = edpi.order_id\n" +
            "where  edpi.deleted = 0 and edpi.status = 0 and edp.integrate_id = #{integraId}\n" +
            "group  by    edpi.product_category_id,edpi.product_category_name,edpi.supplier_id , edpi.product_unit_id,edpi.product_unit_name , edpi.product_standards ,edpi.product_name,edpi.product_id ")
    List<DepPurchaseItemDO> getByPriWarehouseIntegraId(String integraId);

    @Select("SELECT\n" +
            "\tedpi.dept_id dept_id,\n" +
            "\tedpi.product_name product_name,\n" +
            "\tedpi.supplier_id supplier_id,\n" +
            "\tedpi.order_id order_id,\n" +
            "\tedpi.id id,\n" +
            "\tsum( edpi.count_actual ) count_actual \n" +
            "FROM\n" +
            "\terp_dep_purchase edp\n" +
            "\tLEFT JOIN erp_dep_purchase_item edpi ON edp.id = edpi.order_id \n" +
            "WHERE\n" +
            "\tedpi.deleted = 0 \n" +
            "\tAND edp.integrate_id = #{integraId} \n" +
            "\tAND edpi.product_standards = #{productStandards}\n" +
            "\tAND edpi.product_unit_id = #{productUnitId}\n" +
            "\tAND edpi.product_name = #{productName}\n" +
            "GROUP BY\n" +
            "\tedp.dept_id,\n" +
            "\tedpi.order_id,\n" +
            "\tedpi.id,\n" +
            "\tedpi.supplier_id,\n" +
            "\tedpi.product_name")
    List<DepPurchaseItemDO> getDepPurchaseItemList(String integraId,Long supplierId,Long productUnitId,String productName,String productStandards);

    @Update(" update erp_dep_purchase_item set supplier_id =  #{supplierId},supplier_dept_id = #{supplierDeptId} " +
            "where product_id = #{productId} And product_unit_id = #{productUnitId} And product_standards = #{productStandards} ")
    void updateDepPurchaseItemSupplier(DepPurchaseItemSaveReqVO updateReqVO);

    default List<DepPurchaseItemDO> getDepPurchaseItemByOrderId(List<Long> orderIds){
         return selectList(DepPurchaseItemDO::getOrderId,orderIds);
    }
}